package com.pansoft.kline.adapter;

import androidx.annotation.Nullable;
import android.view.ContextMenu;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.efounder.utils.JfResourceUtil;
import com.efounder.utils.ResStringUtil;
import com.pansoft.kline.model.KlineOptionalOrMVModel;
import com.pansoft.kline.util.SelfSelectUtils;
import com.pansoft.openplanet.R;

import java.util.List;

/**
 * @author : zzj
 * @e-mail : zhangzhijun@pansoft.com
 * @date : 2018/11/19 10:00
 * @desc : 自选和市值的adapter
 * @version: 1.0
 */
public class KlineOptionalOrMVAdapter extends BaseQuickAdapter<KlineOptionalOrMVModel,KlineOptionalOrMVAdapter.ContextMenuViewHolder> {

    private List<KlineOptionalOrMVModel> data;

    public KlineOptionalOrMVAdapter(int layoutResId, @Nullable List<KlineOptionalOrMVModel> data) {
        super(layoutResId, data);
        this.data = data;
    }

    @Override
    protected void convert(KlineOptionalOrMVAdapter.ContextMenuViewHolder helper, KlineOptionalOrMVModel item) {
        helper.setText(R.id.tv_title,item.getTitle());
        helper.setText(R.id.tv_name,item.getName());
        helper.setText(R.id.tv_price,item.getPrice());
        helper.setText(R.id.tv_num,item.getNum());
        helper.setText(R.id.tv_sub_price,item.getSubPrice());
        helper.setText(R.id.btn_change_rate,item.getChangeRate());
        if(item.isIncrease()){
            helper.setBackgroundRes(R.id.btn_change_rate,R.drawable.kline_shape_btn_item__change_green_bg);
        }else {

            helper.setBackgroundRes(R.id.btn_change_rate,R.drawable.kline_shape_btn_item__change_red_bg);
        }
        //稳定
        if(item.getTrendType() == 0){
            helper.setTextColor(R.id.tv_price,JfResourceUtil.getSkinColor(R.color.chart_main_white_or_black));
            helper.setTextColor(R.id.tv_sub_price, JfResourceUtil.getSkinColor(R.color.black_text_color));
            //涨
        }else if(item.getTrendType() == 1){
            helper.setTextColor(R.id.tv_price,JfResourceUtil.getSkinColor(R.color.increasing_color));
            helper.setTextColor(R.id.tv_sub_price,JfResourceUtil.getSkinColor(R.color.increasing_color));
        }else if(item.getTrendType() == -1){
            //跌
            helper.setTextColor(R.id.tv_price,JfResourceUtil.getSkinColor(R.color.decreasing_color));
            helper.setTextColor(R.id.tv_sub_price,JfResourceUtil.getSkinColor(R.color.decreasing_color));
        }
    }

    public class ContextMenuViewHolder extends BaseViewHolder implements View.OnCreateContextMenuListener{

        public ContextMenuViewHolder(View view) {
            super(view);
            view.setOnCreateContextMenuListener(this);
        }


        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
            //        getActivity().getMenuInflater().inflate(R.menu.self_select_menu, menu);
            final long id = SelfSelectUtils.getIsSelectedId(data.get(getAdapterPosition()).getMarketId());
            if (id != -1) {
                menu.add(0, v.getId(), getAdapterPosition(), ResStringUtil.getString(R.string.chart_delete_self_selected));
            } else {
                menu.add(0, v.getId(), getAdapterPosition(), ResStringUtil.getString(R.string.chart_add_to_self_selected));
            }
        }
    }
}
